//package com.plian.system.service.fr.internalauditmanagement.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.dto.fr.internalauditmanagement.IamWorkYearAuditProjecMasterDTO;
//import com.plian.system.dto.fr.internalauditmanagement.IamWorkYearAuditProjectDTO;
//import com.plian.system.entity.fr.internalauditmanagement.IamWorkYearAuditProject;
//import com.plian.system.entity.fr.internalauditmanagement.IamWorkYearAuditProjectMaster;
//import com.plian.system.entity.sys.Org;
//import com.plian.system.mapper.fr.internalauditmanagement.IamWorkYearAuditProjectMapper;
//import com.plian.system.mapper.fr.internalauditmanagement.IamWorkYearAuditProjectMasterMapper;
//import com.plian.system.model.ObjResult;
//import com.plian.system.model.R;
//import com.plian.system.config.exception.MyRuntimeException;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.fr.internalauditmanagement.IamWorkYearRecordMasterService;
//import com.plian.system.service.fr.internalauditmanagement.IamWorkYearRecordService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.fr.internalauditmanagement.IamWorkYearAuditProjectMasterVO;
//import com.plian.system.wrapper.fr.internalauditmanagement.IamWorkYearAuditProjectMasterWrapper;
//import com.plian.system.wrapper.fr.internalauditmanagement.IamWorkYearAuditProjectWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * @author jiatp
// * @create 2023-02-20
// **/
//@Service
//@Slf4j
//@AllArgsConstructor
//public class IamWorkYearRecordMasterServiceImpl extends BaseServiceImpl<IamWorkYearAuditProjectMasterMapper, IamWorkYearAuditProjectMaster> implements IamWorkYearRecordMasterService, BaseFlowService {
//
//
//    private FormWorkflowService formWorkflowService;
//    private IFormCodeService formCodeService;
//    private final IOrgService orgService;
//    private final IamWorkYearAuditProjectMasterMapper auditProjectMasterMapper;
//
//    private final IamWorkYearAuditProjectMapper auditProjectMapper;
//
//    private final IamWorkYearRecordService workYearRecordService;
//
//    private final IamWorkYearAuditProjectMasterWrapper auditProjectMasterWrapper;
//    private final IamWorkYearAuditProjectWrapper auditProjectWrapper;
//
//    @Transactional(rollbackFor = Throwable.class)
//    @Override
//    public R submit(IamWorkYearAuditProjectMasterVO recordVO) {
//        try {
//            IamWorkYearAuditProjectMaster auditProjectMaster = auditProjectMasterWrapper.voToEntity(recordVO);
//            Org org = orgService.getById(TokenUtil.getTokenOrgId());
//            if (auditProjectMaster.getId() == null) {
//                auditProjectMaster.setEnterpriseId(recordVO.getEnterpriseId());
//                auditProjectMaster.setEnterpriseName(org.getName());
//                auditProjectMaster.setCode(formCodeService.getCode(FormTypeConstant.IAM_WORK_YEAR_REPORT_RECORD));
//                auditProjectMaster.setFormType(String.valueOf(FormTypeConstant.IAM_WORK_YEAR_REPORT_RECORD));
//            }
//            super.saveOrUpdate(auditProjectMaster);
//
//            //本年度
//            saveBatchProject(auditProjectWrapper.voToEntity(recordVO.getAuditProjects()), auditProjectMaster);
//            //下年度
//            saveBatchProject(auditProjectWrapper.voToEntity(recordVO.getPlanAuditProjects()), auditProjectMaster);
//
//            return R.success();
//        } catch (Exception e) {
//            log.error("error-submit:" + e.getMessage());
//            throw new MyRuntimeException(new ObjResult("操作失败"));
//
//        }
//    }
//
//    /**
//     * 保存和修改
//     *
//     * @param list
//     * @param auditProjectMaster
//     * @return void
//     **/
//    private void saveBatchProject(List<IamWorkYearAuditProject> list, IamWorkYearAuditProjectMaster auditProjectMaster) {
//        if (CollectionUtil.isNotEmpty(list)) {
//            list.forEach(item -> {
//                item.setAuditId(auditProjectMaster.getId());
//                item.setEnterpriseId(auditProjectMaster.getEnterpriseId());
//                item.setEnterpriseName(auditProjectMaster.getEnterpriseName());
//                item.setFillingYear(auditProjectMaster.getFillingYear());
//                item.setFillingTime(auditProjectMaster.getFillingTime());
//                item.setCode(formCodeService.getCode(FormTypeConstant.IAM_WORK_YEAR_REPORT_RECORD));
//                item.setFormType(String.valueOf(FormTypeConstant.IAM_WORK_YEAR_REPORT_RECORD));
//            });
//            workYearRecordService.saveList(list);
//            List<IamWorkYearAuditProject> delItems = list.stream().filter(item -> item.getIsDeleted() == 1).collect(Collectors.toList());
//            if (CollectionUtil.isNotEmpty(delItems)) {
//                List<Long> ids = delItems.stream().map(IamWorkYearAuditProject::getId).collect(Collectors.toList());
//                workYearRecordService.deleteLogic(ids);
//            }
//        }
//    }
//
//    @Override
//    public IamWorkYearAuditProjecMasterDTO detail(IamWorkYearAuditProjectMasterVO auditProjectMasterVO) {
//
//        IamWorkYearAuditProjectMaster auditProjectMaster = getById(auditProjectMasterVO.getId());
//
//        if (auditProjectMaster != null) {
//            IamWorkYearAuditProjecMasterDTO masterDTO = auditProjectMasterWrapper.entityToDto(auditProjectMaster);
//
//            List<IamWorkYearAuditProjectDTO> auditProjectDTOList = new ArrayList<>();
//            List<IamWorkYearAuditProjectDTO> planAuditProjectDTOList = new ArrayList<>();
//            //本年度
//            List<IamWorkYearAuditProject> auditProjectList = auditProjectMapper.selectList(new LambdaQueryWrapper<IamWorkYearAuditProject>()
//                    .eq(IamWorkYearAuditProject::getAuditId, auditProjectMaster.getId()).eq(IamWorkYearAuditProject::getIsPlan, 1));
//            if (CollectionUtil.isNotEmpty(auditProjectList)) {
//                auditProjectDTOList = auditProjectWrapper.entityToDto(auditProjectList);
//            }
//            //下年度
//            List<IamWorkYearAuditProject> planAuditProjectsList = auditProjectMapper.selectList(new LambdaQueryWrapper<IamWorkYearAuditProject>()
//                    .eq(IamWorkYearAuditProject::getAuditId, auditProjectMaster.getId()).eq(IamWorkYearAuditProject::getIsPlan, 2));
//            if (CollectionUtil.isNotEmpty(planAuditProjectsList)) {
//                planAuditProjectDTOList = auditProjectWrapper.entityToDto(planAuditProjectsList);
//            }
//            masterDTO.setAuditProjects(auditProjectDTOList);
//            masterDTO.setPlanAuditProjects(planAuditProjectDTOList);
//            return masterDTO;
//        }
//        return null;
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            IamWorkYearAuditProjectMaster master = getById(id);
//            if (master == null) {
//                return false;
//            } else if (master.getStatus() != null &&
//                    (master.getStatus() != FormStatusConstant.STASH && master.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//            formWorkflowService.deleteProcessInstance(String.valueOf(id));
//        }
//        boolean result = super.deleteLogic(ids);
//        return result;
//    }
//
//    @Override
//    public HashMap<String, Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String, Object>();
//    }
//
//    public Boolean finish(String id) {
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        List<IamWorkYearAuditProjectMaster> masterList = list(new LambdaQueryWrapper<IamWorkYearAuditProjectMaster>()
//                .in(IamWorkYearAuditProjectMaster::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA))));
//        List<IamWorkYearAuditProjectMasterVO> masterVOS = auditProjectMasterWrapper.entityToVO(masterList);
//        List<PageData> pageDataList = new ArrayList<>();
//        try {
//            if (CollectionUtil.isNotEmpty(masterVOS)) {
//                for (IamWorkYearAuditProjectMasterVO vo : masterVOS) {
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", vo.getEnterpriseName());
//                    pd.put("APPLICANTNAME", vo.getCreateUser());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", vo.getEnterpriseId());
//                    pd.put("formType", FormTypeConstant.IAM_WORK_YEAR_REPORT_RECORD);
//                    pd.put("orgName", vo.getEnterpriseName());
//                    pageDataList.add(pd);
//                }
//            }
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(FormTypeConstant.IAM_WORK_YEAR_REPORT_RECORD);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     *
//     * @param id           主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            IamWorkYearAuditProjectMaster master = getById(id);
//            if (updateStatus == FormStatusConstant.FINISH) {
//                master.setApprovedUser(TokenUtil.getTokenUserId());
//                Date now = DateUtil.now();
//                master.setApprovedTime(now);
//            }
//            master.setStatus(updateStatus);
//            updateById(master);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//
//}
